﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.APP.App_UI.UserControls;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class ESS_IntegrateProject : System.Web.UI.Page
    {
        //double sumBath = 0;
        Main main = new Main();
        IBLL_BGM_MAS_Option opt = new B_BGM_MAS_Option();
        IBLL_BGM_ProjectOperation op = new B_BGM_ProjectOperation();
        IBLL_BGM_ProjectOperationExpense opexp = new B_BGM_ProjectOperationExpense();
        IBLL_BGM_ProjectOperationReport oprpt = new B_BGM_ProjectOperationReport();
        IBLL_BGM_ProjectReport pjrpt = new B_BGM_ProjectReport();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //================= Test =================
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                //lblWorkAreaSeq.Text = "1";
                //========================================

                LoadDDLBgmOptionSeq();
            }
        }

        protected void LoadDDLBgmOptionSeq()
        {
            string strUserJobsiteSeq = Session["UserInternalJobsiteSeq"].ToString();
            ddlBgmOptionSeq.DataSource = opt.GetIntegrateProjectList(strUserJobsiteSeq);
            ddlBgmOptionSeq.DataBind();

            //RadComboBoxItem empty = new RadComboBoxItem("โครงการภายใต้กิจกรรม", "");
            //empty.TemplateControl.Controls.Add()
            //ddlBgmOptionSeq.Items.Insert(0, empty);
            //main.InsertEmptyDDLSelect(ddlBgmOptionSeq);
        }

        private void LoadRoot()
        {
            string strBgmOptionSeq = ddlBgmOptionSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            IList<M_BGM_ProjectOperation> infolist = op.GetListByBgmOptionSeq(strBgmYear, strBgmOptionSeq);

            Reperter1.DataSource = infolist;
            Reperter1.DataBind();
        }

        //=================================== Events ===========================================================
        protected void ddlBgmOptionSeq_DataBound(object sender, EventArgs e)
        {
            ((Literal)ddlBgmOptionSeq.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(ddlBgmOptionSeq.Items.Count);
        }

        protected void ddlBgmOptionSeq_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = ((M_BGM_MAS_Option)e.Item.DataItem).bgm_option_text;
            e.Item.Value = ((M_BGM_MAS_Option)e.Item.DataItem).bgm_option_seq.ToString();
        }

        protected void ddlBgmOptionSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRoot();
        }

        protected void Reperter1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((BGM_ProjectOperation)e.Item.FindControl("BGM_ProjectOperation1")).Visible = false;
                ((ImageButton)e.Item.FindControl("cmdUp")).Visible = false;
                ((ImageButton)e.Item.FindControl("cmdDown")).Visible = true;

                if (((Label)e.Item.FindControl("lblRecordStatus")).Text != "A")
                {
                    ((LinkButton)e.Item.FindControl("cmdApprove")).Visible = true;
                    ((LinkButton)e.Item.FindControl("cmdClose")).Visible = false;
                }
                else
                {
                    ((LinkButton)e.Item.FindControl("cmdApprove")).Visible = false;
                    ((LinkButton)e.Item.FindControl("cmdClose")).Visible = true;
                }
            }
        }

        protected void Reperter1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ImageButton cmdUp = (ImageButton)e.Item.FindControl("cmdUp");
            ImageButton cmdDown = (ImageButton)e.Item.FindControl("cmdDown");
            Label lblBgmProjOperSeq = (Label)e.Item.FindControl("lblBgmProjOperSeq");
            BGM_ProjectOperation BGM_ProjectOperation1 = (BGM_ProjectOperation)e.Item.FindControl("BGM_ProjectOperation1");
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;
            M_BGM_ProjectOperation ProjectInfo = new M_BGM_ProjectOperation();

            switch (e.CommandName)
            {
                case "Up" :
                    BGM_ProjectOperation1.Visible = false;
                    cmdUp.Visible = false;
                    cmdDown.Visible = true;
                    break;
                case "Down" :
                    if (BGM_ProjectOperation1.Get_lblBgmProjOperSeq().Text.Length == 0)
                    {
                        BGM_ProjectOperation1.LoadUserControl(lblBgmProjOperSeq.Text);
                    }
                    BGM_ProjectOperation1.Visible = true;
                    cmdUp.Visible = true;
                    cmdDown.Visible = false;
                    break;
                case "Approve" :
                    
                    string strBgmProjectSeq = lblBgmProjOperSeq.Text;
                    string strBgmProjectName = "";
                    IList<M_BGM_ProjectOperationExpense> ExpInfo = opexp.GetFull(strBgmProjectSeq, null, null);
                    strBgmProjectName = ProjectInfo.bgm_proj_oper_name;

                    for (int i = 0; i < ExpInfo.Count; i++)
                    {
                        string strBgmProjExpSeq = ExpInfo[i].bgm_proj_exp_seq.ToString();
                        string strBgmProjOperExpSeq = ExpInfo[i].bgm_proj_oper_exp_seq.ToString();
                        string strBgmExpName = ExpInfo[i].m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_name;
                        double dAmount = ExpInfo[i].amount.Value;
                        double dRemainAmount = pjrpt.GetCalculateList(null, strBgmProjExpSeq, null, null, null
                            , null, null, null, null, null, strBgmProjOperExpSeq)[0].cal_amount.Value;

                        if (dAmount > dRemainAmount)
                        {
                            lblError.Text = "จำนวนเงินคงเหลือของ [" + strBgmExpName + "] ไม่พออนุมัติ";
                            return;
                        }
                        else
                        {
                            lblError.Text = "";
                        }

                        M_BGM_ProjectReport ProjRptInfo = new M_BGM_ProjectReport();
                        ProjRptInfo.descr = "" + " - " + strBgmProjectName;
                        ProjRptInfo.bgm_proj_exp_seq = main.ConvertNullInt(strBgmProjExpSeq);
                        ProjRptInfo.bgm_proj_oper_exp_seq = main.ConvertNullInt(strBgmProjOperExpSeq);
                        ProjRptInfo.used_amount = dAmount;
                        ProjRptInfo.cal_amount = -dAmount;
                        ProjRptInfo.rpt_type = 4;
                        ProjRptInfo.record_status = "W";
                        ProjRptInfo.created_by = main.ConvertNullInt(strUserID);
                        ProjRptInfo.created_when = dtNow;

                        pjrpt.Insert(ProjRptInfo);

                        M_BGM_ProjectOperationReport OperRptInfo = new M_BGM_ProjectOperationReport();
                        OperRptInfo.descr = "" + " - " + strBgmProjectName;
                        OperRptInfo.bgm_proj_oper_exp_seq = main.ConvertNullInt(strBgmProjOperExpSeq);
                        OperRptInfo.used_amount = dAmount;
                        OperRptInfo.cal_amount = dAmount;
                        OperRptInfo.rpt_type = 1;
                        OperRptInfo.record_status = "W";
                        OperRptInfo.created_by = main.ConvertNullInt(strUserID);
                        OperRptInfo.created_when = dtNow;

                        oprpt.Insert(OperRptInfo);
                    }

                    //ProjectInfo.bgm_proj_oper_seq = Convert.ToInt32(strBgmProjectSeq);
                    //ProjectInfo.record_status = "A";
                    //ProjectInfo.updated_by = main.ConvertNullInt(strUserID);
                    //ProjectInfo.updated_when = dtNow;

                    //pj.ChangeStatus(ProjectInfo);
                    //AddNew();

                    ProjectInfo.bgm_proj_oper_seq = Convert.ToInt32(lblBgmProjOperSeq.Text);
                    ProjectInfo.record_status = "A";
                    ProjectInfo.updated_by = main.ConvertNullInt(strUserID);
                    ProjectInfo.updated_when = dtNow;

                    op.ChangeStatus(ProjectInfo);
                    LoadRoot();
                    break;
                case "Close" :
                    //ProjectInfo.bgm_proj_oper_seq = Convert.ToInt32(lblBgmProjOperSeq.Text);
                    //ProjectInfo.updated_by = main.ConvertNullInt(strUserID);
                    //ProjectInfo.updated_when = dtNow;

                    //op.ChangeStatus(ProjectInfo);
                    break;
            }
        }
    }
}
